/**
 * 
 */
package com.chainwintel.prjmgmt.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import com.chainwintel.prjmgmt.model.MenuItem;
import com.chainwintel.prjmgmt.service.MenuItemMgr;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

/**
 * @author dearshor
 *
 */
@Component
@Results({
	@Result(name = "menuForm", location = "menuEditForm.jsp"),
	@Result(name = "listMenus", location = "menu_list", type = "redirectAction"),
	@Result(name = "menuListPage", location = "menuManagement.jsp")

})
@ExceptionMappings({
	@ExceptionMapping(exception = "com.chainwintel.prjmgmt.exception.FailedToAssignOperationsException" ,result = "error.jsp"),
	@ExceptionMapping(exception = "java.lang.RuntimeException", result = "error.jsp"),
	@ExceptionMapping(exception = "java.lang.Exception" ,result = "error.jsp")
})
public class MenuAction extends ActionSupport implements Preparable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -6337356626939544963L;
	
	private static final Log log = LogFactory.getLog(UserAction.class);
	
	private MenuItem menuItem;
	
	private long menuId = -1;
	
	private List<MenuItem> menuList;
	
	@Autowired private MenuItemMgr menuItemMgr;
	
	
	
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		setMenuList(getMenuItemMgr().listMenus());
	}
	
	@Action(value = "/menu_edit_form")
	public String prepareUserForm() {
		// TODO Auto-generated method stub
		try {
			if (getMenuId() < 0) {
				setMenuItem(new MenuItem());
			} else {
				setMenuItem(getMenuItemMgr().find(getMenuId()));
			}
			return "menuForm";
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			log.error("DataAccessException occurred", e);
			throw new RuntimeException("Operation failed.",e);
		}
	}
	
	@Action("/menu_edit")
	public String doMenuEdit() {
		try {
			getMenuItemMgr().merge(getMenuItem());
			return "listMenus";
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			log.error("DataAccessException occurred", e);
			throw new RuntimeException("Operation failed.",e);
		}
	}
	
	

	@Action("/menu_list")
	public String showMenus() {
		return "menuListPage";
	}

	@Action("menu_delete")
	public String  doMenuDelete() {
		try {
			MenuItem item = getMenuItemMgr().find(getMenuId());
			getMenuItemMgr().remove(item);
			return "listMenus";
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			log.error("DataAccessException occurred", e);
			throw new RuntimeException("Operation failed.",e);
		}
	}
	
	/**
	 * @param menuItem the menuItem to set
	 */
	public void setMenuItem(MenuItem menuItem) {
		this.menuItem = menuItem;
	}

	/**
	 * @return the menuItem
	 */
	public MenuItem getMenuItem() {
		return menuItem;
	}

	/**
	 * @param menuId the menuId to set
	 */
	public void setMenuId(long menuId) {
		this.menuId = menuId;
	}

	/**
	 * @return the menuId
	 */
	public long getMenuId() {
		return menuId;
	}

	/**
	 * @param menuList the menuList to set
	 */
	public void setMenuList(List<MenuItem> menuList) {
		this.menuList = menuList;
	}

	/**
	 * @return the menuList
	 */
	public List<MenuItem> getMenuList() {
		return menuList;
	}

	public void setMenuItemMgr(MenuItemMgr menuItemMgr) {
		this.menuItemMgr = menuItemMgr;
	}

	public MenuItemMgr getMenuItemMgr() {
		return menuItemMgr;
	}

}
